package com.wcpdoc.exam.core.service.impl;

import com.wcpdoc.base.em.LogType;
import com.wcpdoc.base.service.OpLogService;
import com.wcpdoc.core.dao.BaseDao;
import com.wcpdoc.core.entity.PageResult;
import com.wcpdoc.core.entity.PageResultEx;
import com.wcpdoc.core.service.impl.BaseServiceImp;
import com.wcpdoc.exam.core.dao.ExmMyExerDao;
import com.wcpdoc.exam.core.entity.ExmMyExer;
import com.wcpdoc.exam.core.service.ExmMyExerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ExmMyExerServiceImpl extends BaseServiceImp<ExmMyExer> implements ExmMyExerService {

    @Resource
    private ExmMyExerDao exmMyExerDao;
    @Resource
    private OpLogService opLogService;

    @Override
    @Resource(name = "exmMyExerDaoImpl")
    public void setDao(BaseDao<ExmMyExer> dao) {
        super.dao = dao;
    }


    /**
     * 先判断有没有这条数据，如果有就修改，没有就新增
     * @param exmMyExer
     * @return
     */
    @Override
    public PageResult save(ExmMyExer exmMyExer) {
        ExmMyExer entity = exmMyExerDao.get(exmMyExer.getUserId(), exmMyExer.getType());
        if(entity!=null){
            entity.setContent(exmMyExer.getContent());
            entity.setUpdateTime(new Date());
            dao.update(entity);
        }else{
            exmMyExer.setUpdateTime(new Date());
            dao.add(exmMyExer);
        }
        Integer logType = exmMyExer.getType()==1?LogType.JC_LX.getValue():LogType.ZX_LX.getValue();
        new Thread(() -> opLogService.log(exmMyExer.getUserId(),logType)).start();
        return PageResultEx.ok();
    }

    @Override
    public PageResult get(Integer userId, Integer type) {
        ExmMyExer exmMyExer= exmMyExerDao.get(userId,type);
        if(exmMyExer!=null){
            return PageResultEx.ok().data(exmMyExer.getContent());
        }
        return PageResultEx.ok().data("");
    }
}
